#include "kernel_operator.h"

constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue

class KernelSoftplusV2 {
public:
  __aicore__ inline KernelSoftplusV2() {}
  __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, uint32_t totalLength,
                              uint32_t tileNum) {
    this->blockLength = totalLength / AscendC::GetBlockNum();
    this->tileNum = tileNum;
    this->tileLength = this->blockLength / tileNum / BUFFER_NUM;

    xGm.SetGlobalBuffer((__gm__ DTYPE_X *)x +
                            this->blockLength * AscendC::GetBlockIdx(),
                        this->blockLength);
    zGm.SetGlobalBuffer((__gm__ DTYPE_Z *)z +
                            this->blockLength * AscendC::GetBlockIdx(),
                        this->blockLength);

    pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(DTYPE_X));
    pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(DTYPE_Z));
  }

  __aicore__ inline void Process() {
    int32_t loopCount = this->tileNum * BUFFER_NUM;
    for (int32_t i = 0; i < loopCount; i++) {
      CopyIn(i);
      Compute(i);
      CopyOut(i);
    }
  }

private:
  __aicore__ inline void CopyIn(int32_t progress) {
    AscendC::LocalTensor<DTYPE_X> xLocal = inQueueX.AllocTensor<DTYPE_X>();
    AscendC::DataCopy(xLocal, xGm[progress * this->tileLength],
                      this->tileLength);
    inQueueX.EnQue(xLocal);
  }

  __aicore__ inline void Compute(int32_t progress) {
    AscendC::LocalTensor<half> xLocal = inQueueX.DeQue<half>();
    AscendC::LocalTensor<half> yLocal = outQueueZ.AllocTensor<half>();
    AscendC::LocalTensor<half> tmpLocal = inQueueX.AllocTensor<half>();

    // tmpLocal = exp(xLocal)
    AscendC::Exp(tmpLocal, xLocal, tileLength);

    // yLocal = 1.0
    for (uint32_t i = 0; i < tileLength; ++i) {
      yLocal.SetValue(i, static_cast<half>(1.0f));
    }

    // tmpLocal = exp(xLocal) + 1
    AscendC::Add(tmpLocal, tmpLocal, yLocal, tileLength);

    // yLocal = ln(tmpLocal)
    AscendC::Ln(yLocal, tmpLocal, tileLength);

    outQueueZ.EnQue<half>(yLocal);

    inQueueX.FreeTensor(xLocal);
    inQueueX.FreeTensor(tmpLocal);
  }

  __aicore__ inline void CopyOut(int32_t progress) {
    AscendC::LocalTensor<DTYPE_Z> zLocal = outQueueZ.DeQue<DTYPE_Z>();
    AscendC::DataCopy(zGm[progress * this->tileLength], zLocal,
                      this->tileLength);
    outQueueZ.FreeTensor(zLocal);
  }

private:
  AscendC::TPipe pipe;
  AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
  AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
  AscendC::GlobalTensor<DTYPE_X> xGm;
  AscendC::GlobalTensor<DTYPE_Z> zGm;
  uint32_t blockLength;
  uint32_t tileNum;
  uint32_t tileLength;
};

extern "C" __global__ __aicore__ void
soft_plus_v2_custom(GM_ADDR x, GM_ADDR z, GM_ADDR workspace, GM_ADDR tiling) {
  GET_TILING_DATA(tiling_data, tiling);
  KernelSoftplusV2 op;
  op.Init(x, z, tiling_data.totalLength, tiling_data.tileNum);
  op.Process();
}

#ifndef ASCENDC_CPU_DEBUG
extern "C" __aicore__ void soft_plus_v2_custom_do(uint32_t blockDim,
                                                  void *l2ctrl, void *stream,
                                                  uint8_t *x, uint8_t *z,
                                                  uint8_t *workspace,
                                                  uint8_t *tiling) {
  soft_plus_v2_custom<<<blockDim, l2ctrl, stream>>>(x, z, workspace, tiling);
}
#endif